<include file="header" />
<body>
<div class="wraper">
    <div class="header">
        <a href="javascript:history.go(-1)" class="iconfont icon-zuo back"></a>
        <div class="title text-center font-16">绑定虚拟e卡</div>
    </div>
    <div class="classify content margin-t12">
        <div class="content-list flex">
            <div class="user-hint">姓名</div>
            <div>{BEESCRM:$bind['user_name']}</div>
        </div>
        <div class="content-list flex">
            <div class="user-hint">工号</div>
            <div>{BEESCRM:$bind['number']}</div>
        </div>
        <!--<div class="content-list flex">
            <div class="user-hint">身份证号</div>
            <div class="input">
                <input type="text" id="last_six_bit" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')"  placeholder="请填写身份证后六位">
            </div>
        </div>-->
    </div>
    <div class="classify content margin-t12">
        <div class="content-list flex">
            <!--<div class="user-hint">手机号码</div>
            <div class="input">
                <input id="mobile" value="{BEESCRM:$bind['mobile']}" />
            </div>-->
            <div class="user-hint">手机号码</div>
            <div class="input" id="mobile">{BEESCRM:$bind['mobile']}</div>
            <a href="#" class="blue send show font-12">获取验证码</a>
        </div>
        <div class="content-list flex">
            <div class="user-hint">验证码</div>
            <div class="input">
                <input type="text" id="code" placeholder="请填写验证码">
            </div>
        </div>
    </div>
    <a href="#" class="btn text-center font-16">绑定</a>
    <notempty name="storedValue['notice']">
        <div class="hint  gray ">
            <p class="red">温馨提示：</p>
            <p class="red">{BEESCRM:$storedValue['notice']}</p>
        </div>
    </notempty>
</div>
<include file="commonJs" />
</body>
<script>
    var height = $(window).height();
    $('.wraper').css('minHeight', height);

    //点击获取验证码
    var verifyTag = true;
    $('.content-list').on('click', '.send', function (e) {
        e.preventDefault();
        if (verifyTag)
            getCode(this);
        else
            return;
    });

    // 发送验证码
    function getCode(elem) {
        var data = {};
        data.mobile = $('#mobile').html();
        var stored_value_id = "{BEESCRM:$stored_value_id}";
        var verify = /^(?:13\d|15\d|18\d|17\d|14\d)\d{5}(\d{3}|\*{3})$/;
        if (!verify.test(data.mobile)) {
            fm.alert('请输入正确的手机号码');
            return false;
        }else{
            $.ajax({
                url:"/base/StoredValue/sendText?stored_value_id="+stored_value_id,
                type:'POST',
                dataType:'json',
                data:data,
                async: false,
                success:function (res) {
                    if(res.status == 'S'){
                        timeCountDown(elem);
                    } else {
                        fm.alert(res.message);
                    }
                }
            });
            timeCountDown(elem);
        }
    }

    // 验证倒计时
    function timeCountDown(elem, seconds) {
        verifyTag = false;
        if (typeof seconds == undefined || seconds == null) seconds = 60;
        setTimeout(function () {
            if (seconds > 0) {
                seconds--;
                $(elem).text('已发送 ' + seconds + 's').addClass('time');
                timeCountDown(elem, seconds);
            } else {
                $(elem).removeClass('time').text('获取验证码');
                verifyTag = true;
            }
        }, 1000);
    }

    //绑定
    $('.btn').click(function () {
        var bind_id = "{BEESCRM:$bind['bind_id']}";
        var mobile = $("#mobile").html();
        var code = $("#code").val();
        var verify = /^(?:13\d|15\d|18\d|17\d|14\d)\d{5}(\d{3}|\*{3})$/;
        if (!verify.test(mobile)) {
            fm.alert('手机号格式错误');
            return false;
        }

        if(!code ){
            fm.alert("请填写验证码");
            return false;
        }

        if(!code || !/^[0-9]{6}$/.test(code)){
            fm.alert("验证码格式不正确");
            return false;
        }

        var stored_value_id = "{BEESCRM:$stored_value_id}";
        $.ajax({
            url: '/base/StoredValue/doBound/stored_value_id/'+ stored_value_id,
            type: "POST",
            dataType: "json",
            data: {bind_id: bind_id, mobile: mobile,code:code},
            success: function (res) {
                if(res.status == 'S'){
                    fm.alert(res.message, "", 2, "");
                    window.sessionStorage.setItem("local_url","/base/StoredValue/personalCenter/stored_value_id/"+ stored_value_id);
                    setTimeout(redirect, 2000);
                } else if(res.status == 'BS'){
                    fm.alert(res.message, "", 2, "");
                    window.sessionStorage.setItem("local_url",res.url);
                    var test = window.sessionStorage.getItem("local_url");
                    console.log(test);
                    setTimeout(redirect, 2000);
                } else {
                    fm.alert(res.message);
                }
            }
        });
    });

    function redirect() {
        window.location.href = window.sessionStorage.getItem("local_url");
        window.sessionStorage.setItem("local_url",null);
    }
</script>
</html>